<template>
  <div class="evaluations-container">
    <el-table :data="evaluationsList" stripe style="width: 100%">
      <el-table-column prop="evaluationId" label="评价ID" width="100" />
      <el-table-column prop="userId" label="用户ID" width="100" />
      <el-table-column prop="appointmentId" label="预约ID" width="100" />
      <el-table-column prop="score" label="评分" width="100">
        <template #default="{ row }">
          <el-rate v-model="row.score" disabled />
        </template>
      </el-table-column>
      <el-table-column prop="comment" label="评价内容" />
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(row.evaluationId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { queryAll, deleteById } from '@/api/evaluations'

interface Evaluation {
  evaluationId: number
  userId: number
  appointmentId: number
  score: number
  comment: string
}

const evaluationsList = ref<Evaluation[]>([])

const loadEvaluations = async () => {
  try {
    const res = await queryAll()
    evaluationsList.value = res.data
  } catch (error) {
    ElMessage.error('获取评价列表失败')
  }
}

const handleDelete = (id: number) => {
  ElMessageBox.confirm('确定要删除该评价吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteById(id)
      ElMessage.success('删除成功')
      loadEvaluations()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

onMounted(() => {
  loadEvaluations()
})
</script>

<style lang="scss" scoped>
.evaluations-container {
  padding: 20px;
}
</style>
